package com.motong.gongdan.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.motong.gongdan.entity.StockMsg;
import com.motong.gongdan.mapper.StockMsgMapper;
import com.motong.gongdan.vo.H5Vo.stock.query.StockListQuery;
import com.motong.gongdan.vo.H5Vo.stock.vo.StockListVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author:fff
 * @time:2022/3/8
 */
@Service
public class StockMsgService extends ServiceImpl<StockMsgMapper, StockMsg> {

    @Resource
    private StockDetailService stockDetailService;

    /**
     * 获取打印数据
     *
     * @param stockMsgId 库存id
     */
    public Map<String, Object> getExportData(Integer stockMsgId) {
        return this.baseMapper.getExportData(stockMsgId);
    }

    /**
     * 根据库存id获取详情
     *
     * @param stockMsgId 库存id
     */
    public List<Map<String, Object>> getExportProductList(Integer stockMsgId) {
        return this.baseMapper.getExportProductList(stockMsgId);
    }

    /**
     * h5端获取库存列表
     *
     * @param query 查询条件
     */
    public IPage<StockListVo> h5StockList(Page<StockListVo> page, StockListQuery query) {
        if (Objects.isNull(query.getMinTime()) || Objects.isNull(query.getMaxTime())) {
            query.setDateCompType(null);
        }
        return this.baseMapper.h5StockList(page, query);
    }


    /**
     * 通过库存编码删除库存信息
     *
     * @param stockCode 差值
     */
    public void removeStockDetailBy(List<String> stockCode) {
        if (CollectionUtils.isEmpty(stockCode)) {
            return;
        }
        List<StockMsg> stockMsgList = this.list(new LambdaQueryWrapper<StockMsg>().in(StockMsg::getCode, stockCode));
        List<Integer> stockMsgIds = stockMsgList.stream().map(StockMsg::getId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(stockMsgIds)) {
            stockDetailService.removeStockDetailByMsgId(stockMsgIds);
            this.removeByIds(stockMsgIds);
        }
    }
}
